package cn.wolfcode.web.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {
    @Autowired
    private ISystemDictionaryService  systemDictionaryService;
    @RequestMapping("/list")
    @RequiredPermission(name="数据字典查询",expression = "systemDictionary:list")
    public String list(Model model,QueryObject qo){
        List<SystemDictionary> systemDictionaries = systemDictionaryService.quryTree();
        model.addAttribute("systemDictionaries",systemDictionaries);
        return "systemDictionary/list";


//                List<SystemDictionary> systemDictionarys = systemDictionaryService.listAll();
//        model.addAttribute("systemDictionarys",systemDictionarys);
//        return "systemDictionary/list";  //找视图 / WEB-INF/views/systemDictionary/list.jsp
    }
    @RequestMapping("/delete")
    @RequiredPermission(name="数据字典删除",expression = "systemDictionary:delete")
    public String delete(Long id){
        if (id!=null) {
            systemDictionaryService.delete(id);
        }
        return "redirect:/systemDictionary/list";  //让浏览器重新发一次请求
    }
    @RequestMapping("/input")
    @RequiredPermission(name="数据字典新增或修改",expression = "systemDictionary:input")
    public String input(Long id,Model model){
        if (id!=null) {
            SystemDictionary systemDictionary = systemDictionaryService.get(id);
            model.addAttribute("systemDictionary",systemDictionary);
        }
        return "systemDictionary/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="数据字典新增或修改",expression = "systemDictionary:saveOrUpdate")
    public String saveOrUpdate(SystemDictionary systemDictionary){
        if (systemDictionary.getId()!=null) {
            systemDictionaryService.update(systemDictionary);
        }else {
          systemDictionaryService.save(systemDictionary);
        }
        return "redirect:/systemDictionary/list";
    }
    @RequestMapping("/queryItemById")
    @ResponseBody
    public  List<SystemDictionary> queryItemById(Long id){
        return systemDictionaryService.queryItemById(id);
    }










}
